//
//  MYXAuthenticationViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/6.
//  Copyright © 2017年 hbjApple. All rights reserved.
//实名认证界面

#import "MYXAuthenticationViewController.h"
#import "MYXAuthenticationCell.h"
#import "MYXAuthenticationTopView.h"
#import "MYXAuthenticationBottomView.h"
#import "MYXPhotoauthenticationViewController.h"
#import "MYXMemberMdel.h"
#import "MYXAuthenticationModel.h"


static NSString *const MYXAuthenticationCellIdentifier = @"MYXAuthenticationCell";
static NSString *const MYXAuthenticationTopViewIdentifier = @"MYXAuthenticationTopView";
static NSString *const MYXAuthenticationBottomViewIdentifier = @"MYXAuthenticationBottomView";
@interface MYXAuthenticationViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIAlertViewDelegate,UIAlertViewDelegate,UITextFieldDelegate>{
    NSMutableArray *titleArray;//文字数组
}
@property(nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong)MYXAuthenticationTopView *MYXAuthenticationTopV;
@property (nonatomic, strong)MYXAuthenticationBottomView *MYXAuthenticationBottomV;
@property(nonatomic, copy)NSString *PhotoStr;
@property(nonatomic, copy)NSString *videoStr;
@property(nonatomic, strong) MYXHttpManager *MYXHttpManag;
@property(nonatomic, strong) MYXAuthenticationModel *MYXAuthenticationM;
@end

@implementation MYXAuthenticationViewController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    if (_MYXAuthenticationM==nil) {
        _MYXAuthenticationM = [[MYXAuthenticationModel alloc]init];
    }
}
#pragma mark - Data
-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

#pragma mark - load


-(void)setMYXMemberMd:(MYXMemberMdel *)MYXMemberMd{
    _MYXMemberMd = MYXMemberMd;
    if ([MYXMemberMd.is_idauth isEqualToString:@"1"]){
        [self idauth:@"" code:@"" image_front:@"" image_back:@""];
    }
    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
     WS(weakSelf)
    _PhotoStr =  _PhotoStr.length>0 ? _PhotoStr : @"请上传认证照片";
    _videoStr =  _videoStr.length>0 ? _videoStr : @"请上传认证视频";
    
    
    titleArray= [NSMutableArray arrayWithArray:@[
                                     @{@"title": @"真实姓名",
                                            @"placeholder" : @"请填写真实姓名" },
                                         @{@"title": @"证件类型",
                                            @"placeholder" : @"身份证" },
                                         @{@"title": @"证件号码",
                                            @"placeholder" : @"请填写证件号码" },
                                         @{ @"title": @"照片认证",
                                            @"placeholder" : _PhotoStr },
//                                         @{ @"title": @"视频认证",
//                                            @"placeholder" :_videoStr},
                                     ]
                 ];

    self.tableView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:MYXAuthenticationCellIdentifier bundle:nil] forCellReuseIdentifier:MYXAuthenticationCellIdentifier];
     self.MYXAuthenticationTopV                                =[[NSBundle mainBundle]loadNibNamed:MYXAuthenticationTopViewIdentifier owner:self options:nil][0];
    self.MYXAuthenticationTopV.frame = CGRectMake(0, 0, _tableView.frame.size.width,20);
    self.tableView.tableHeaderView =self.MYXAuthenticationTopV;
    
    
    if (![self.MYXMemberMd.is_idauth isEqualToString:@"1"]){
        self.MYXAuthenticationBottomV                                =[[NSBundle mainBundle]loadNibNamed:MYXAuthenticationBottomViewIdentifier owner:self options:nil][0];
        self.MYXAuthenticationBottomV.frame = CGRectMake(0, 0, _tableView.frame.size.width,60);
        self.tableView.tableFooterView =self.MYXAuthenticationBottomV;
        self.MYXAuthenticationBottomV.sureB = ^{
            GRLog(@"确认提交");
            if (weakSelf.MYXAuthenticationM.real_name.length==0){
                [weakSelf showNoReceivertext:@"请填写真实姓名"];
                return;
            }if (weakSelf.MYXAuthenticationM.code.length==0){
                [weakSelf showNoReceivertext:@"请填写身份证号码"];
                return;
            }if (weakSelf.MYXAuthenticationM.image_front.length==0){
                [weakSelf showNoReceivertext:@"请上传身份证正面照片"];
                return;
            }if (weakSelf.MYXAuthenticationM.image_back.length==0){
                [weakSelf showNoReceivertext:@"请上传身份证反面照片"];
                return;
            }
            
            [weakSelf idauth:weakSelf.MYXAuthenticationM.real_name code:weakSelf.MYXAuthenticationM.code image_front:weakSelf.MYXAuthenticationM.image_front image_back:weakSelf.MYXAuthenticationM.image_back];
        };

    }
    
    if ([_MYXMemberMd.is_idauth isEqualToString:@"-1"]){
        self.MYXAuthenticationTopV.Label.text = @"您的信息由于填写有误，请重新填写";
        self.MYXAuthenticationTopV.Label.textColor =[UIColor redColor];
    }else{
        self.MYXAuthenticationTopV.Label.text = @"请填写您的真实信息，通过后不能更改";
    }
    
}


#pragma mark - tableView


- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithRed:0.94f green:0.94f blue:0.96f alpha:1.00f];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
    }
    return _tableView;
}
#pragma mark - tableview datasource and delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MYXAuthenticationCell *MYXAuthenticationC = (MYXAuthenticationCell *)[tableView dequeueReusableCellWithIdentifier:MYXAuthenticationCellIdentifier];
    MYXAuthenticationC.selectionStyle = UITableViewCellSelectionStyleNone;
    MYXAuthenticationC.titleLabel.text = titleArray[indexPath.row][@"title"];
    MYXAuthenticationC.textFiled.placeholder = titleArray[indexPath.row][@"placeholder"];
    MYXAuthenticationC.textFiled.delegate  = self;
    if (indexPath.row==4||indexPath.row==3) {
        MYXAuthenticationC.textFiled.enabled = NO;
        MYXAuthenticationC.arrowImage.hidden = NO;
        if (indexPath.row==4) {
            MYXAuthenticationC.linView.hidden = YES;
        }else{
            if (self.MYXAuthenticationM.image_front.length>0&& self.MYXAuthenticationM.image_back.length>0) {
                MYXAuthenticationC.textFiled.placeholder = @" 已上传照片";
            }
        }
    }else{
        if (indexPath.row==1) {
            MYXAuthenticationC.textFiled.enabled = NO;
        }else{
            MYXAuthenticationC.textFiled.enabled = YES;
            if (indexPath.row==0) {
                 MYXAuthenticationC.textFiled.text=self.MYXAuthenticationM.real_name;
            }else if (indexPath.row==2){
                 MYXAuthenticationC.textFiled.text=self.MYXAuthenticationM.code;
            }
           
        }
       
        MYXAuthenticationC.arrowImage.hidden = YES;
    }
     MYXAuthenticationC.textFiled.tag = 1000+indexPath.row;
    
    return MYXAuthenticationC;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 49;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row==3) {
        MYXPhotoauthenticationViewController *MYXPhotoauthenticationVC =[[MYXPhotoauthenticationViewController alloc]init];
        if (self.MYXAuthenticationM == nil) {
            self.MYXAuthenticationM = [[MYXAuthenticationModel alloc] init];
        }
        MYXPhotoauthenticationVC.authenticationMod= self.MYXAuthenticationM;
        MYXPhotoauthenticationVC.navigationItem.title = @"照片认证";
        MYXPhotoauthenticationVC.PhotoauthenBlock =^(MYXAuthenticationModel *MYXAuthenticationM){
            self.MYXAuthenticationM.image_front = MYXAuthenticationM.image_front;
            self.MYXAuthenticationM.image_back = MYXAuthenticationM.image_back;
            NSIndexPath *indexpatch = [NSIndexPath indexPathForRow:3 inSection:0];
            [self.tableView reloadRowsAtIndexPaths:@[indexpatch] withRowAnimation:UITableViewRowAnimationAutomatic];
            //[self.tableView reloadData];
        };
        [self.navigationController pushViewController:MYXPhotoauthenticationVC animated:YES];
    }
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView =[[UIView alloc]init];
    headView.backgroundColor =[UIColor colorWithRed:0.95f green:0.95f blue:0.95f alpha:1.00f];
    headView.frame = CGRectMake(0, 0, WIDTH, 0.1);
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *FooterView =[[UIView alloc]init];
    FooterView.frame = CGRectMake(0, 0, WIDTH, 0.1);
    return FooterView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.tag ==1000) {
        self.MYXAuthenticationM.real_name =textField.text;
        
    }else if (textField.tag ==1002){
        self.MYXAuthenticationM.code =textField.text;
    }

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - Request

-(void)idauth:(NSString *)real_name code:(NSString *)code image_front:(NSString *)image_front image_back:(NSString *)image_back{
    
    self.MYXHttpManag =[[MYXHttpManager alloc]init];
    [self.MYXHttpManag idauth:real_name
                         code:code
                  image_front:image_front
                   image_back:image_back
                      success:^(NSString *dataStr,MYXAuthenticationModel *MYXAuthenticationMo) {
//        if (MYXAuthenticationMo) {
//             self.MYXAuthenticationM =[[MYXAuthenticationModel alloc]init];
//             self.MYXAuthenticationM =MYXAuthenticationMo;
            [self showTopMessage:dataStr];
            [self initMemberRequest];//更新个人数据
            dispatch_time_t timer =    dispatch_time(  DISPATCH_TIME_NOW, 1.5 * NSEC_PER_SEC);
            dispatch_after(timer, dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });


//        }else{
//            [self showTopMessage:dataStr];
//        }
           

   
         
        [self.tableView reloadData];
    } failure:^(NSString *errorCode) {
        [self showTopMessage:errorCode];
    } netDown:^{
        [self showTopMessage:@"网络不可用"];
    }];
}
#pragma mark - 没有输入任何内容的提示
-(void)showNoReceivertext:(NSString *)message
{
    [JXTHUDHelper showHUDText:message type:@"warning" inView:nil];
}
#pragma mark - 我的个人资料
-(void)initMemberRequest
{
    [self.MYXHttpManag userinfo:^(MYXMemberMdel *user) {
//        if (user) {
//         //   self.MYXMemberMde =user;
//
//            [self.tableView reloadData];
//        }
    } failure:^(NSString *errorCode) {
     //   [self showServerErrorOfFeedList];
    } netDown:^{
      //  [self showServerErrorOfFeedList];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
